package com.banana.manager.forest.service.impl.chemical;

import com.banana.manager.forest.dao.chemical.IDisburseDao;
import com.banana.manager.forest.dao.chemical.IOutDao;
import com.banana.manager.forest.dao.handle.IDiscussDao;
import com.banana.manager.forest.entity.chemical.DisburseBean;
import com.banana.manager.forest.entity.chemical.OutBean;
import com.banana.manager.forest.entity.handle.DiscussBean;
import com.banana.manager.forest.entity.handle.RecordBean;
import com.banana.manager.forest.service.iidevice.IDisburseService;
import com.banana.manager.forest.service.iihandle.IDiscussService;
import com.banana.manager.forest.util.PageInfoSize;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class DisburseServiceImpl implements IDisburseService {

    @Resource
    private IDisburseDao disburseDao;

    @Resource
    private IOutDao outDao;

    @Override
    public DisburseBean addDisburse(DisburseBean disburseBean, List<OutBean> outBeans) {
        disburseDao.insert(disburseBean);
        for(int i = 0; i<outBeans.size();i++){
            outDao.insert(outBeans.get(i));
            outDao.confirmDevice(outBeans.get(i).getOutId(),outBeans.get(i).getDeviceBean().getDeviceId());
            outDao.addToDisburse(outBeans.get(i).getOutId(),disburseBean.getDisburseId());
        }
        return disburseDao.findOneById(disburseBean.getDisburseId());
    }

    @Override
    public PageInfo<DisburseBean> findBlurPage(Integer page, LocalDate start, LocalDate end, String disburseClass) {
        String disburseClassNew = "%"+disburseClass+"%";
        QueryWrapper<DisburseBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("disburse_date",start);
        queryWrapper.le("disburse_date",end);
        queryWrapper.like("disburse_class",disburseClassNew);
        PageHelper.startPage(page, PageInfoSize.pageSize);
        return PageInfo.of(disburseDao.selectList(queryWrapper));
    }

    public DisburseBean findOneDis(Integer disburseId){
        return disburseDao.findOneById(disburseId);
    }
}
